import Banner from "../Banner";
import { 头盔, 手枪, 防弹衣, 突击步枪, 冲锋枪, 射手步枪, 栓动步枪, 轻机枪, 霰弹枪, 卡宾枪, 投掷物, 子弹, 药品, 食物, } from "../Datas/Data";
import { Constant, ItemType } from "../Framework/Const/Constant";
import AudioManager from "../Framework/Managers/AudioManager";
import { DataManager } from "../Framework/Managers/DataManager";
import { EventManager, MyEvent } from "../Framework/Managers/EventManager";
import { PoolManager } from "../Framework/Managers/PoolManager";
import { UIManager } from "../Framework/Managers/UIManager";
import NodeUtil from "../Framework/Utils/NodeUtil";
import Item from "./Item";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ShopPanel extends cc.Component {

    MoneyLabel: cc.Label = null;

    Buttons: cc.Node = null;
    ItemContent: cc.Node = null;

    type: ItemType;
    items: cc.Node[] = [];

    protected onLoad(): void {
        this.MoneyLabel = NodeUtil.GetComponent("MoneyLabel", this.node, cc.Label);
        this.Buttons = NodeUtil.GetNode("Buttons", this.node);
        this.ItemContent = NodeUtil.GetNode("ItemContent", this.node);
    }

    Show(type: ItemType = ItemType.头盔) {
        this.RefreshMoney();
        AudioManager.PlayAudio(Constant.Audio.ButtonClick);
        this.type = type;
        this.RefreshButtons();
        this.RefreshItems();
    }

    RefreshItems() {
        this.items.forEach(e => PoolManager.PutNode(e));

        let datas = DataManager.ItemDatas.get(this.type);
        for (let i = 0; i < datas.length; i++) {
            const data = datas[i];
            const type = this.type;
            PoolManager.GetNode("Prefabs/UI/Item", this.ItemContent).then(e => {
                let item = e.getComponent(Item);
                this.items.push(e);
                switch (type) {
                    case ItemType.头盔: item.头盔(data as 头盔); break;
                    case ItemType.防弹衣: item.防弹衣(data as 防弹衣); break;
                    case ItemType.手枪: item.手枪(data as 手枪); break;
                    case ItemType.突击步枪: item.突击步枪(data as 突击步枪); break;
                    case ItemType.冲锋枪: item.冲锋枪(data as 冲锋枪); break;
                    case ItemType.射手步枪: item.射手步枪(data as 射手步枪); break;
                    case ItemType.栓动步枪: item.栓动步枪(data as 栓动步枪); break;
                    case ItemType.轻机枪: item.轻机枪(data as 轻机枪); break;
                    case ItemType.霰弹枪: item.霰弹枪(data as 霰弹枪); break;
                    case ItemType.卡宾枪: item.卡宾枪(data as 卡宾枪); break;
                    case ItemType.投掷物: item.投掷物(data as 投掷物); break;
                    case ItemType.子弹: item.子弹(data as 子弹); break;
                    case ItemType.药品: item.药品(data as 药品); break;
                    case ItemType.食物: item.食物(data as 食物); break;
                }
            })
        }
    }

    RefreshButtons() {
        this.Buttons.children.forEach(e => {
            let selected = e.name == ItemType[this.type];
            e.getChildByName("Selected").active = selected;
        })
    }

    RefreshMoney() {
        this.MoneyLabel.string = `${DataManager.Money}`;
    }

    OnButtonClick(event: cc.Event) {
        AudioManager.PlayAudio(Constant.Audio.ButtonClick);
        this.type = ItemType[`${event.target.name}`];
        this.RefreshButtons();
        this.RefreshItems();
    }

    OnAddMoneyButtonClick() {
        AudioManager.PlayAudio(Constant.Audio.ButtonClick);
        Banner.Instance.ShowVideoAd(() => {
            AudioManager.PlayAudio(Constant.Audio.GetMoney);
            DataManager.Money += 20000;
        });
    }

    OnReturnButtonClick() {
        AudioManager.PlayAudio(Constant.Audio.ButtonClick);
        UIManager.Instance.HidePanel(Constant.Panel.ShopPanel);
    }

    protected onEnable(): void {
        EventManager.on(MyEvent.RefreshMoney, this.RefreshMoney, this)
    }

    protected onDisable(): void {
        EventManager.off(MyEvent.RefreshMoney, this.RefreshMoney, this)
    }
}
